package com.sec.android.app.myfiles.module.search;

import android.app.LoaderManager;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.provider.MediaStore;
import android.text.TextUtils;

import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.CategoryType;
import com.sec.android.app.myfiles.module.local.category.CategoryFileRecord;
import com.sec.android.app.myfiles.module.local.category.CategoryListAdapterImp;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo.COLUMN_ID;
import com.sec.android.app.myfiles.util.StorageMonitor;
import com.sec.android.app.myfiles.util.UiUtils;

public class CategorySearchAdapter extends SearchAdapter {

    public CategorySearchAdapter(Context context, NavigationInfo navigationInfo, LoaderManager loaderManager) {
        super(context, navigationInfo, loaderManager);
    }

    @Override
    protected Cursor createGroupCursor() {
        MatrixCursor groupCursor = new MatrixCursor(GROUP_PROJECTION);
        groupCursor.addRow(new String[]{
                String.valueOf(SEARCH_STORAGE_ID.CATEGORY), getDisplayName()});

        return groupCursor;
    }

    private String getDisplayName() {
        String displayName;
        if (UiUtils.isCategoryFolderList(mPreRecord)) {
            displayName = StorageMonitor.getDisplayName(mContext, mPreRecord.getFullPath());
        } else {
            if(mPreRecord instanceof  CategoryFileRecord) {
                displayName = ((CategoryFileRecord) mPreRecord).getCategoryName(mContext, (CategoryFileRecord) mPreRecord);
            }
            else {
                displayName = mPreRecord.getPath().replace("/", "");
            }
        }

        return displayName;
    }

    @Override
    protected String getSelection(int id) {
        StringBuilder selection = new StringBuilder();
        addSelectionQuery(selection);
        CategoryType categoryType = getCategoryType();

        if (!TextUtils.isEmpty(selection)) {
            selection.append(" AND ");
        }

        switch (categoryType) {
            case Image:
                selection.append(mSearchDbTableInfo.getColumnName(COLUMN_ID.FILE_TYPE)).append('=');
                selection.append(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE);
                break;
            case Video:
                selection.append(mSearchDbTableInfo.getColumnName(COLUMN_ID.FILE_TYPE)).append('=');
                selection.append(MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO);
                break;
            case Audio:
                selection.append(mSearchDbTableInfo.getColumnName(COLUMN_ID.FILE_TYPE)).append('=');
                selection.append(MediaStore.Files.FileColumns.MEDIA_TYPE_AUDIO);
                break;
            case Document:
                selection.append(CategoryListAdapterImp.getDocumentSelection());
                break;
            case Apk:
                selection.append(MediaStore.Files.FileColumns.DATA).append(" LIKE '%.apk'");
                break;
            default:
                break;
        }

        if (!mShowHidden) {
            selection.append(" AND ").append(mSearchDbTableInfo.getColumnName(COLUMN_ID.PATH)).append(" NOT LIKE '%/.%'");
        }

        return selection.toString();
    }

    private CategoryType getCategoryType() {
        CategoryType categoryType;
        if (UiUtils.isCategoryFolderList(mPreRecord)) {
            categoryType = CategoryFileRecord.getCategoryType(mPreRecord.getFullPath());
        } else {
            categoryType = CategoryFileRecord.getCategoryType(FileRecord.getFullPath(mPreRecord.getPath(), mPreRecord.getName()));
        }

        return categoryType;
    }
}